package com.ruipengkj.commons.util.DBDump;

import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruidom.Com.Transform.Util.Updater;
import com.ruidom.util.PrintWriter;
import com.ruipengkj.jdbc.ConnectionNetWork;

/**
 * Created with IntelliJ IDEA. User: Administrator Date: 12-5-16 Time: 上午12:59
 * To change this template use File | Settings | File Templates.
 */
public class XMLDataExport {
	private static String packageVersion = "1";// 包格式版本
	public static Map<String, String> tableKeys = null;

	public static Map<String, String> tableKeys() {
		if (tableKeys == null) {
			synchronized (XMLDataExport.class) {
				if (tableKeys == null) {
					try {
						tableKeys = new HashMap<String, String>();
						Connection conn = new ConnectionNetWork().getConnection();// DBHelper.GetInstance().getConnection();
						Statement stmt = conn.createStatement();
						ResultSet rs = stmt.executeQuery("show tables;");
						
						List<String> name = new ArrayList<String>();
						while (rs.next()) {
							String str = rs.getString(1);
							if (str.contains("view") == true) {
//								System.out.println(str+ "---------------------");
							} else {
								name.add(str);
							}
						}
						
						for (String s:name) {
							ResultSet rs2= stmt.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='"+s+"'");
							rs2.next();
							tableKeys.put(s, rs2.getString(1));
						}
						
					} catch (Exception e) {
						e.printStackTrace(); // To change body of catch
												// statement use File | Settings
												// | File Templates.
					}
				}
			}
		}

		return tableKeys;
	}

	public void export(PrintWriter _os, Connection _conn, List<String> _tables)
			throws SQLException {
		PrintWriter os = _os;
		Connection conn = _conn;
		Statement stmt = conn.createStatement();
		conn.setAutoCommit(false);// 导出数据是一个事务

		os.print("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
				.print("<package packageVersion=\"").print(packageVersion)
				.print("\">");
		for (String table : _tables) {
			ResultSet rs = stmt.executeQuery(getDataSQL(table));
			try {
				Updater.output(os, rs, 1, "name=\"" + table + "\"");
			} catch (IOException e) {
				e.printStackTrace();
			}
			rs.close();
		}
		stmt.close();
		conn.commit();
		os.print("</package>");
	}

	protected String getDataSQL(String _talbe) {
		return "SELECT * from " + _talbe;
	}

	public static void main(String[] args) {
		try {
			PrintWriter pw = null;
			pw = new PrintWriter(new java.io.PrintWriter(new FileWriter(
					"test.xml")));
			Connection conn = new ConnectionNetWork().getConnection();// DBHelper.GetInstance().getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("show tables;");
			List<String> tables = new ArrayList<String>();
			while (rs.next()) {
				String str = rs.getString(1);
				if (str.contains("view") == true) {
//					System.out.println(str + "---------------------");
				} else {
					// System.out.println(str);
					tables.add(str);
				}
			}
			new XMLDataExport().export(pw, conn, tables);
			conn.close();
			pw.close();
		} catch (Exception e) {
			e.printStackTrace(); // To change body of catch statement use File |
									// Settings | File Templates.
		}
	}
}
